import BaseUtil from '@/BaseUtil';
import LightningEffects from '@/LightningEffects';
import MathUtil from '@/MathUtil';
import SelectUtil from '@/SelectUtil';
import { OrderId } from '@/globals/order';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 生命汲取
 **/
export default class BossSpell_StellLife extends AbilityBase {
    static instance: BossSpell_StellLife;
    constructor(id: string = 'Mzw4') {
        super(id);
        BossSpell_StellLife.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
    }
    init() {}
    lights: Map<unit, lightning> = new Map();
    effects: Map<unit, effect> = new Map();
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        BossAbilityManagerInstance.text('生命汲取', source);
        let es = SelectUtil.getEnemyUnitsInRange(source, 750);
        this.lights.clear();
        for (let e of es) {
            let ef = AddLightningEx(
                LightningEffects.sheng_ming_ji_qu,
                true,
                GetUnitX(source),
                GetUnitY(source),
                Math.max(GetUnitFlyHeight(source), 20),
                GetUnitX(e),
                GetUnitY(e),
                Math.max(GetUnitFlyHeight(e), 20)
            );
            this.lights.set(e, ef);
            let eff = AddSpecialEffectTarget('Abilities\\Spells\\Other\\Drain\\DrainTarget.mdl', e, 'chest');
            this.effects.set(e, eff);
        }
        let eff = AddSpecialEffectTarget('Abilities\\Spells\\Other\\Drain\\DrainTarget.mdl', source, 'origin');
        this.effects.set(source, eff);
        BaseUtil.onTimer(0.1, (c) => {
            if (GetUnitCurrentOrder(source) == OrderId.Drain) {
                for (let e of es) {
                    let dis = MathUtil.distanceBetweenUnits(e, source);
                    if (dis < 950) {
                        let ef = this.lights.get(e);
                        MoveLightningEx(ef, true, GetUnitX(source), GetUnitY(source), Math.max(GetUnitFlyHeight(source), 20), GetUnitX(e), GetUnitY(e), Math.max(GetUnitFlyHeight(e), 20));
                        DamageSystemInstance.applyDarkDamage(e, source, 300);
                        DamageSystemInstance.applyHeal(source, source, 300);
                    } else {
                        DestroyLightning(this.lights.get(e));
                        this.lights.delete(e);
                    }
                }
            } else {
                for (let [key, value] of this.lights) {
                    DestroyLightning(value);
                    this.lights.delete(key);
                }
                for (let [key, value] of this.effects) {
                    DestroyEffect(value);
                    this.effects.delete(key);
                }
                return false;
            }
            if (c >= 80) {
                for (let [key, value] of this.lights) {
                    DestroyLightning(value);
                    this.lights.delete(key);
                }
                for (let [key, value] of this.effects) {
                    DestroyEffect(value);
                    this.effects.delete(key);
                }
            }

            return c < 80;
        });
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
}
